package com.ruoyi.carShopService.controller;

import com.ruoyi.carShopService.service.LoginService;
import com.ruoyi.carShopService.service.OperateStatisticsService;
import com.ruoyi.carShopService.util.JwtUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.api.RemoteStatisticsService;
import com.ruoyi.system.api.domain.CarMerchantPrincipal;
import com.ruoyi.system.api.domain.CarOrders;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/statistics")
@CrossOrigin
public class OperateStatisticsController extends BaseController {
    @Autowired
    private OperateStatisticsService operateStatisticsService;
    @Autowired
    private RemoteStatisticsService remoteStatisticsService;

    @Autowired
    private LoginService loginService;

    private String merchantStr;
    @GetMapping("/list")
    public TableDataInfo list(CarOrders carOrders)
    {
        startPage();
        if (merchantStr != null){
            carOrders.setMerchantId(Long.parseLong(merchantStr));
        }
        List<Map<String, Object>> list = operateStatisticsService.selectSumMoney(carOrders);
        System.out.println(list);
        return getDataTable(list);
    }

    @GetMapping("/orderCount")
    public TableDataInfo count(CarOrders carOrders)
    {
        List<Map<String, Object>> list = operateStatisticsService.selectOrderCount(carOrders);
        System.out.println(list);
        return getDataTable(list);
    }

    @GetMapping("/getTodayDingdan")
    public ResponseEntity<Map<String, Object>> getCarTodayDingdan()
    {
        ResponseEntity<Map<String, Object>> carTodayDingdan = remoteStatisticsService.getCarTodayDingdan();
        return carTodayDingdan;
    }


    @RequestMapping("/loadInfo")
    public AjaxResult loadInfo(@RequestHeader("token") String token){
        System.out.println(token);
        // 解析token
        try{
            Claims claims = JwtUtil.parseJWT(token);
            merchantStr = claims.getSubject();
            System.out.println(merchantStr);
            return AjaxResult.success("加载信息成功",merchantStr);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
